﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FindLinkRegister
{
    public class WebsiteDAO
    {
        /// <summary>
        /// Lấy tất cả dữ liệu 
        /// </summary>
        /// <returns>List<website></returns>
        public static List<WebsiteDTO> GetAllLink()
        {
            try
            {
                List<WebsiteDTO> results = new List<WebsiteDTO>();
                string sql = "SELECT * FROM Website WHERE IsDeleted=0 AND IsActived=1";
                
                DataProvider.Connnect();
                DataTable dt = new DataTable();
                dt = DataProvider.ExcuteQuery(sql);
                if (dt == null)
                    return null;
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    WebsiteDTO website = new WebsiteDTO();
                    website.LinkWebsite = (string)dt.Rows[i]["Link"];
                    website.LinkResult = "";//******************************************
                    website.IsDeleted = 0;
                    results.Add(website);
                }

                return results;
            }
            catch (Exception ex)
            {
                throw ex;
                //return null;
            }
            finally
            {
                DataProvider.Disconect();
            }
        }

        /// <summary>
        /// Update website, update LinkRegister
        /// </summary>
        /// <param name="url">Link website</param>
        /// <param name="website">New website</param>
        /// <returns>1: ok; 0: not ok</returns>
        public static int UpdateLinkRegister(string url, WebsiteDTO website)
        {
            int result = 0;
            try
            {
                string sql = "UPDATE Website"
                            + " SET LinkRegister='" + website.LinkResult + "'"
                            + " WHERE Link='" + url + "'";
                DataProvider.Connnect();
                result = DataProvider.ExcuteNonQuery(sql);
            }
            catch (Exception ex)
            {
                //result = 0;
                throw ex;
            }
            finally
            {
                DataProvider.Disconect();
            }
            return result;
        }

        /// <summary>
        /// Update website, update LinkLogin
        /// </summary>
        /// <param name="url">Link website</param>
        /// <param name="website">New website</param>
        /// <returns>1: ok; 0: not ok</returns>
        public static int UpdateLinkLogin(string url, WebsiteDTO website)
        {
            int result = 0;
            try
            {
                string sql = "UPDATE Website"
                            + " SET LinkLogin='" + website.LinkResult + "'"
                            + " WHERE Link='" + url + "'";
                DataProvider.Connnect();
                result = DataProvider.ExcuteNonQuery(sql);
            }
            catch (Exception ex)
            {
                //result = 0;
                throw ex;
            }
            finally
            {
                DataProvider.Disconect();
            }
            return result;
        }
    }
}
